import { Module, Store } from 'vuex';
import { IRootState } from '..';
import axios from '../../utils/request';
import Axios from 'axios';
interface IUser {
  account: string;
  token: string;
}
export interface IUserState {
  userInfo: IUser;
  picUrl:string
}

export default () => {
  const user: Module<IUserState, IRootState> = {
    namespaced: true,
    state: {
      userInfo: {
        account: '',
        token: ''
      },
      picUrl:'111'
    },
    getters: {
      isLogin(state) {
        return !!state.userInfo.token;
      }
    },
    mutations: {
      updateToken(state, token: string) {
        state.userInfo.token = token;
      },
      updateUser(state, user: IUser) {
        state.userInfo = user;
      },
      upUrl(state,pic:string){
        console.log(pic);
        state.picUrl = pic;
      }
    },
    actions: {
      updateToken({ commit }, token: string) {
        commit('updateToken', token);
      },
      async getDePic({commit}){
        try {
          const {data} = await Axios.get('http://localhost:5656/pic');
          console.log(data);
          if(data.data){
            commit('upUrl',data.data)
          }
          } catch (error) {
            console.log(error);
          }
      }
    }
  };
  return user;
};
export type IUserStore<S = IUserState> = Store<S>;
